package doublePointers;

/**
 * @author huangxianjin
 * @date 2025/9/11 23:48
 * @description "判断子序列"
 */
public class LC_392 {
    public static void main(String[] args) {
        String s = "abc";
        String t = "ahbgdc";
        new LC_392().isSubsequence(s, t);
    }

    //自己的做法
    public boolean isSubsequence(String s, String t) {
        if (s.isEmpty()) {
            return true;
        }
        if (t.isEmpty()) {
            return false;
        }
        int left = 0;
        int right = 0;

        int index = 0;
        while (left < t.length()) {
            if (t.charAt(left) == s.charAt(right)) {
                ++index;
                ++right;
            }
            if (index == s.length()) {
                return true;
            }
            left++;
        }
        return false;
    }

    //官方做法
    public boolean isSubsequence2(String s, String t) {
        int n = s.length(), m = t.length();
        int i = 0, j = 0;
        while (i < n && j < m) {
            if (s.charAt(i) == t.charAt(j)) {
                i++;
            }
            j++;
        }
        return i == n;
    }
}
